/**
 * projectName: springboot-mybatis
 * fileName: GlobalExceptionHandler.java
 * packageName: com.zb.api.business.exception
 * date: 2022-02-21 14:00
 * copyright(c) 2017-2020 xxx公司
 */
package com.zb.api.business.exception;
/**
 * @version: V1.0
 * @author: fendo
 * @className: GlobalExceptionHandler
 * @packageName: com.zb.api.business.exception
 * @description: 全局异常处理
 * @data: 2022-02-21 14:00
 **/

import com.zb.api.common.models.ServerResponse;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.SocketTimeoutException;

/**
 * @description:
 * @author: User
 * @date: 2022-02-21 14:00
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * jwt信息校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(JwtException.class)
    public ServerResponse jwtExceptionHandle(Exception e){
        log.info(e.getMessage());
        return ServerResponse.error(1001,"登录信息异常,请重新登录");
    }
    @ExceptionHandler(RedisConnectionFailureException.class)
    public ServerResponse socketTimeoutException(Exception e){
        log.info(e.getMessage());
        return ServerResponse.error(1002,"redis连接错误");
    }
    @ExceptionHandler(RuntimeException.class)
    public ServerResponse runtimeException(Exception e){
        e.printStackTrace();
        String message = StringUtils.isEmpty(e.getMessage())?"未知错误":e.getMessage();
        log.info(message);
        return ServerResponse.error(1003,message);
    }


}